package secondBrush.doubles;

import com.sun.source.tree.WhileLoopTree;
import org.junit.internal.builders.NullBuilder;

import java.awt.font.NumericShaper;
import java.security.Key;
import java.util.Arrays;

public class E01Leetcode27 {
    public int removeElement(int[] nums, int val) {
        int result = 0;
        int start = 0;
        int end = nums.length - 1;
        while (start < end) {


            if (nums[start] == val) {
                int temp = nums[start];
                nums[start] = nums[end];
                nums[end] = temp;
                result++;

            }

            start++;
            end--;


        }


        return nums.length -result;

    }
    //??
    public int removeElement2(int[] nums, int val) {
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[result] = nums[i];
                result++;
            }

        }
        return result;

    }

    //双指针
    public int removeElement3(int[] nums, int val) {
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[result] = nums[i];
                result++;
            }

        }
        return result;

    }






    public static void main(String[] args) {
        int[] nums = {0,1,2,2,3,0,4,2};
        int val = 2;
        E01Leetcode27 e01Leetcode27 = new E01Leetcode27();
        System.out.println(e01Leetcode27.removeElement(nums, val));
    }

}
